<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site_m extends CI_Model {

	const TBL = 'site';
	const TBL_USER_SITE = 'user_site';

	/**
     * 构造函数
     *
     * @access public
     * @return void
     */
    public function __construct(){
       parent::__construct();
    }


	function create_site($data){
		if($data){
			if(!$this->_check_xml_url_exist($data['xml_url'])){
				unset($data['launch']);
				if($this->db->insert(self::TBL, $data)){
					return $this->db->insert_id();
				}
			}
		}
		return false;
	}

	function _check_xml_url_exist($xml_url){
		$this->db->select('id');
		$this->db->where('xml_url', $xml_url);
		$query = $this->db->get('site');
		return $query->result();
	}

	function update_site($id, $data)
	{
		if ($id > 0 and $data)
		{
			unset($data->launch);
			$this->db->update('site', $data, "id = $id");
		}
	}

	function delete_site($id){
		$this->db->delete(self::TBL, array('id' => $id));
	}

	function get_paged_site($offset = 0, $row_count = 0, $name = ''){
		$this->db->select('site.*, api_template.name AS template_name');
		$this->db->join('api_template', "api_template.id = site.api_template_id");
		if (strlen($name) > 0)
		{
			$this->db->like('site_name', $name, 'both');
		}
		$this->db->order_by('id desc');
		return $this->db->get(self::TBL, $row_count, $offset);
	}

	function get_total_site($name){
		$this->db->select("COUNT(*) AS total", FALSE);
		if (strlen($name) > 0)
		{
			$this->db->like('site_name', $name, 'both');
		}
		$query = $this->db->get(self::TBL);
		$res = $query->result_array();
		return intval($res[0]['total']);
	}

	function get_site_cap($id){
		if ($id > 0){
			$this->db->select('cap');
			$this->db->where('id', $id);
			$query = $this->db->get(self::TBL);
			if ($query){
				$res = $query->result();
				return intval($res[0]->cap);
			}
		}
		return -1;
	}

    public function get_site($cond = array(), $order = 'id desc')	{
    	foreach ($cond as $key => $val){
			$this->db->where($key, $val);
    	}
    	if($order){
			$this->db->order_by($order);
    	}
		$query = $this->db->get(self::TBL);
		return $query->result();
	}

	public function get_site_by_id($ids = array(), $order = 'weight desc'){
		if(!is_array($ids) || !$ids){
			return array();
		}

		$this->db->select('id, site_name');
		$this->db->where_in('id',$ids);
		if($order){
			$this->db->order_by(trim($order));
    	}
		$query = $this->db->get(self::TBL);
		foreach ($query->result_array() as $row){
			$ret[$row['id']] = $row['site_name'];
		}
		return $ret;
	}

	public function get_all_site(){
		$ret = array();
		$sites = $this->get_site(array(),'weight desc');
		if($sites){
			foreach ($sites as $site){
				$ret[$site->id] = $site->site_name;
			}
		}
		return $ret;
	}

	public function get_craw_sites(){
		$check_time = date("Y-m-d H:i:s",time() - CRAW_INTERVAL * ONE_HOUR);
		$last_failed_time = date("Y-m-d H:i:s",time() - 1 * ONE_HOUR);
		$param = array(
					'status' => 1,
					'last_succ <' => $check_time,
					'last_fail <' => $last_failed_time
				);
		return $this->get_site($param);
	}

	public function update_craw_site_status($site_id, $param){
		if ($site_id < 0){
			return FALSE;
		}
		$now = date("Y-m-d H:i:s",time());
		$this->db->where('id', $site_id);
		$this->db->update('site', $param);
	}

	public function craw_site_succ($site_id){
		$now = date("Y-m-d H:i:s",time());
		$param = array("last_succ" => $now);
		$this->update_craw_site_status($site_id, $param);
	}

	public function craw_site_fail($site_id){
		$now = date("Y-m-d H:i:s",time());
		$param = array("last_fail" => $now);
		$this->update_craw_site_status($site_id, $param);
	}

	function get_user_valid_site($user_id){
    	$ret = array();

    	$this->db->where('user_id', $user_id);
    	$query = $this->db->get(self::TBL_USER_SITE);
    	foreach ($query->result_array() as $row){
    		$ret[] = $row['site_id'];
    	}

    	return $ret;
    }

    function set_user_sites($user_id, $new_sites = array()){
    	$valid_sites = $this->get_user_valid_site($user_id);

    	$insert_arr = array();
    	$del_arr = array();
    	foreach ($new_sites as $new_id){
    		if(!in_array($new_id, $valid_sites)){
    			$insert_arr[] = $new_id;
    		}
    	}
    	foreach ($valid_sites as $old_id){
    		if(!in_array($old_id, $new_sites)){
    			$del_arr[] = $old_id;
    		}
    	}

    	foreach ($insert_arr as $site_id){
    		$data = array(
               'user_id' => $user_id ,
               'site_id' => $site_id
            );
			$this->db->insert(self::TBL_USER_SITE, $data);
    	}

    	$this->db->where('user_id', $user_id);
    	$this->db->where_in('site_id', $del_arr);
    	$this->db->delete(self::TBL_USER_SITE);
    }
}
